﻿using System;
using System.Linq;

class MaxElementInArray

            // Write a method that return the maximal element in a portion of array of integers starting at given index. 
            // Using it write another method that sorts an array in ascending / descending order.

{
    static void Main()
    {
        Console.Write("Enter the size of the array: ");
        int arraySize = int.Parse(Console.ReadLine());
        int[] array = new int[arraySize];
        Console.WriteLine();

        Console.WriteLine("Enter the elements of the array:");
        for (int i = 0; i < arraySize; i++)
        {
            array[i] = int.Parse(Console.ReadLine());
        }
        Console.WriteLine();
        
        Console.Write("Enter the index to start at: ");
        int index = int.Parse(Console.ReadLine());
        Console.WriteLine();
        int[] newArray = new int[arraySize - index];
        Array.Copy(array, index, newArray, 0, arraySize - index);

        int maxElement = newArray.Max();
        Console.WriteLine("The maximal element fro the given index to the end is: {0}", maxElement);
        int[] sortedArray = new int[arraySize];
        Console.WriteLine();
        PrintArr(SortingTheArray(array, sortedArray));
    }

    static int[] SortingTheArray(int[] array, int[] sortedArray)
    {
        for (int i = 0; i < array.Length; i++)
        {
            int[] newArray = new int[array.Length - i];
            Array.Copy(array, i, newArray, 0, array.Length - i);
            int maxElement = newArray.Max();

            if (Array.IndexOf(array, maxElement) < i)
            {
                array[Array.IndexOf(array, maxElement)] = 0;
            }

            int temp = array[Array.IndexOf(array, maxElement)];
            array[Array.IndexOf(array, maxElement)] = array[i];
            array[i] = temp;
            sortedArray[i] = maxElement;
        }
        return sortedArray;
    }

    static void PrintArr(int[] array)
    {
        Console.WriteLine("The sorted array is: ");
        foreach (int number in array)
        {
            Console.Write(number + " ");
        }
        Console.WriteLine();
    }
}

